package com.ias.agdis_r.view;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ias.agdis_r.R;
import com.ias.agdis_r.activity.setting.AppInfoActivity;
import com.ias.agdis_r.activity.setting.GPSDetailActivity;
import com.ias.agdis_r.activity.setting.InfoEffectSettingsActivity;
import com.ias.agdis_r.activity.LoginActivity;
import com.ias.agdis_r.activity.setting.RingerVolumesSettingsActivity;
import com.ias.agdis_r.activity.setting.SettingActivity;
import com.ias.agdis_r.application.AGDISApplication;
import com.ias.agdis_r.bean.auth.AuthEnum;
import com.ias.agdis_r.bean.user.UserInfoBean;
import com.ias.agdis_r.manager.EffectManager;
import com.ias.agdis_r.util.network.HttpUtil;
import com.ias.agdis_r.manager.JavaBeanManager;
import com.ias.agdis_r.manager.ServiceManage;
import com.ias.agdis_r.module.MessageModel;
import com.ias.agdis_r.terminalmanager.restful.base.endpoint.request.BaseRequest;
import com.ias.agdis_r.terminalmanager.restful.security.endpoint.request.LogoutRequest;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu.OnClosedListener;

/**
 * 自定义SlidingMenu 测拉菜单类
 */
public class DrawerView implements OnClickListener {
	private final Activity activity;
	SlidingMenu localSlidingMenu;
	private Button btn_login;
	private ImageView icon_user;
	private TextView t_user;
	private LinearLayout item_1, item_3, item_4, item_5, item_6, item_7, item_10;

	public DrawerView(Activity activity) {
		this.activity = activity;
	}

	public SlidingMenu initSlidingMenu() {
		localSlidingMenu = new SlidingMenu(activity);
		localSlidingMenu.setMode(SlidingMenu.LEFT);// 设置左右滑菜单
		localSlidingMenu.setTouchModeAbove(SlidingMenu.SLIDING_WINDOW);// 设置要使菜单滑动，触碰屏幕的范围
		// localSlidingMenu.setTouchModeBehind(SlidingMenu.SLIDING_CONTENT);//设置了这个会获取不到菜单里面的焦点，所以先注释掉
		localSlidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);// SlidingMenu划出时主页面显示的剩余宽度
		localSlidingMenu.setFadeDegree(0.35F);// SlidingMenu滑动时的渐变程度
		localSlidingMenu.attachToActivity(activity, SlidingMenu.LEFT);// 使SlidingMenu附加在Activity右边
		// localSlidingMenu.setBehindWidthRes(R.dimen.left_drawer_avatar_size);//设置SlidingMenu菜单的宽度
		localSlidingMenu.setMenu(R.layout.drawer_menu);// 设置menu的布局文件
		// localSlidingMenu.toggle();//动态判断自动关闭或开启SlidingMenu
		localSlidingMenu.setOnOpenedListener(new SlidingMenu.OnOpenedListener() {
			public void onOpened() {

			}
		});
		localSlidingMenu.setOnClosedListener(new OnClosedListener() {

			@Override
			public void onClosed() {
				// TODO Auto-generated method stub

			}
		});
		initView();
		return localSlidingMenu;
	}

	private void initView() {
		btn_login = (Button) localSlidingMenu.findViewById(R.id.btn_login);
		icon_user=(ImageView)localSlidingMenu.findViewById(R.id.avater);
		t_user = (TextView) localSlidingMenu.findViewById(R.id.no_login);

		item_1 = (LinearLayout) localSlidingMenu.findViewById(R.id.item_1);
		item_3 = (LinearLayout) localSlidingMenu.findViewById(R.id.item_3);
		item_4 = (LinearLayout) localSlidingMenu.findViewById(R.id.item_4);
		item_5 = (LinearLayout) localSlidingMenu.findViewById(R.id.item_5);
		item_6 = (LinearLayout) localSlidingMenu.findViewById(R.id.item_6);
		item_7 = (LinearLayout) localSlidingMenu.findViewById(R.id.item_7);
		item_10 = (LinearLayout) localSlidingMenu.findViewById(R.id.item_10);
		btn_login.setOnClickListener(this);
		item_1.setOnClickListener(this);
		item_3.setOnClickListener(this);
		item_4.setOnClickListener(this);
		item_5.setOnClickListener(this);
		item_6.setOnClickListener(this);
		item_7.setOnClickListener(this);
		item_10.setOnClickListener(this);
	}

	public void updateUI() {
		String userName = activity.getString(R.string.unlogin);
		icon_user.setImageResource(R.drawable.user_offline);
		String btnText = activity.getString(R.string.login);
		item_1.setVisibility(View.GONE);
		if (ServiceManage.getInstance().getIsLogon()) {
			userName = JavaBeanManager.getInstance().getUserinfo().getEnm();
			btnText = activity.getString(R.string.logout);
			icon_user.setImageResource(R.drawable.user_online);

			//高级设置管理员可用
			//未登录不可用
			item_1.setVisibility(JavaBeanManager.getInstance().getUserinfo().getIsadmin() ? View.VISIBLE: View.GONE);
		}
		t_user.setText(userName);
		btn_login.setText(btnText);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_login:
			// TODO 登录注销不同实现
			if (ServiceManage.getInstance().getIsLogon()) {

				UserInfoBean userinfo = JavaBeanManager.getInstance().getUserinfo();
				boolean userIsAdmin = false;
				if (userinfo != null) {
					userIsAdmin = userinfo.getIsadmin();// 是否为管理员
				}
				//如果是超级管理员登录
				if(userIsAdmin){
					//超级管理员注销
					ServiceManage.getInstance().onLogonOut();
				}
				else {
					// TODO 注销
					BaseRequest request = new LogoutRequest();
					// 发送注销请求
					HttpUtil.getInstance().sendRequest(request, MessageModel.RELO, true);
				}
			} else {
				activity.startActivity(new Intent(activity, LoginActivity.class));
			}

			if (localSlidingMenu != null) {
				if (localSlidingMenu.isMenuShowing()) {
					localSlidingMenu.toggle();
				}
			}
			break;
		case R.id.item_1:
			activity.startActivity(new Intent(activity, SettingActivity.class));
			break;
		case R.id.item_3:
			activity.startActivity(new Intent(activity, InfoEffectSettingsActivity.class));
			break;
		case R.id.item_4:
			activity.startActivity(new Intent(activity, AppInfoActivity.class));
			break;
		case R.id.item_5:
			activity.startActivity(new Intent(activity, RingerVolumesSettingsActivity.class));
			break;
		case R.id.item_6:
				activity.startActivity(new Intent(android.provider.Settings.ACTION_BLUETOOTH_SETTINGS));
				break;
        case R.id.item_7:
            if (JavaBeanManager.getInstance().adjust_pri(AuthEnum.AI_T_GPSINFO.toString()) || JavaBeanManager.getInstance().getUserinfo().getIsadmin()) {
                activity.startActivity(new Intent(activity, GPSDetailActivity.class));
            } else {
                EffectManager.getInstance().ttsPlay(EffectManager.TTS_TYPE_UPTO, activity.getString(R.string.no_AI_T_GPSINFO), false);
            }
			break;
			case  R.id.item_10:
				try{
					AGDISApplication application = (AGDISApplication) activity.getApplication();
					application.reStartAPP(1000);
				}catch (ClassCastException e){

				}

				break;
		default:
			break;
		}

	}
}
